package com.rising.face.bytes.P8_无重复字符的最长子串;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: rising
 * @Description:
 * @Date: create in 2023/1/18 13:30
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int max = 0;//最长
        int startIndex = 0;
        Map<Character, Integer> map = new HashMap<>(); //临时存放字符，用于判重
        for (int i = 0; i < chars.length; i++) {
            if (map.get(chars[i]) != null) { //重复
                startIndex = Math.max(map.get(chars[i]), startIndex);
            }
            max = Math.max(max, i - startIndex + 1) ;
            map.put(chars[i], i + 1);//当前字符下一个位置开始不重复
        }
        return max;
    }
}
